<?php $this->Html->addCrumb('Product Listings','/products'); ?>
<?php $this->Html->addCrumb('Active Products'); ?>
<div class="products">
	<div class="search-title"><?php __('You are in:');?> Active Products. Go to  <?php
		$options=array('unsold'=>'Unsold Products','pending'=>'Pending Products','disapproved'=>'Disapproved Products');
		echo $this->Form->select('action',$options,null,array('empty'=>'--Select--','onchange' => 'redirectaction(this)'));
		?></div>
    <?php echo $this->Form->create('Product', array('action' => 'index')); ?>
<br /><input type="text" name="data[keywords]" value="Enter keywords or item number"  onblur="if(this.value=='') this.value='Enter keywords or item number'" onfocus="if(this.value=='Enter keywords or item number') this.value='';" size="50" />
<select name="data[search_by]">
<option value="">Search BY</option>
<option value="item_title">Item Title</option>
<option value="item_number">Item Number</option>
</select>
<input type="submit" name="searchSubmit" value="Search" />
</form>
	    <input type="button" class="add_selected" value="Add Note to Selected" name="Add Note to Selected" />&nbsp;
        <input type="button" class="end_selected" value="End Selected Listing" name="End Selected Listing" />&nbsp;
        <input type="button" class="make_featured" value="Make Featured" name="Make Selected Featured" />&nbsp;
		<input type="button" class="remove_featured" value="Remove Featured" name="Remove Selected From Featured" />&nbsp;
		<br><br>
        <table cellpadding="0" cellspacing="0" width="100%" id="productlist" class="tablesorter">
            <thead>
	<tr >
			<th><input type="checkbox" id="select_all" /></th>
			<th>&nbsp;</th>
			<th><?php echo $this->Paginator->sort('item #','id');?></th>						
			<th><?php echo $this->Paginator->sort('Products','products_title');?></th>			
			<th><?php echo $this->Paginator->sort('Price','products_price');?></th>			
			<th><?php echo $this->Paginator->sort('Total Hits','products_views');?></th>					
			<th class="actions"><?php __('Actions');?></th>
	</tr>
        </thead>
        <tbody>
	<?php
	//pr($products);
        if(!empty($products)){
	$i = 0;
	foreach ($products as $product):
		$class = ' class="even"';
		
		if ($i++ % 2 == 0) {
			//$class = ' class="odd"';
		}
	?>
	<tr<?php echo $class;?>>
		<td class="pl_borderbottom"><input type="checkbox" id="products_id" value="<?php echo $product['Product']['id']; ?>" /></td>
		<td class="pl_borderbottom">
               <a href="<?php echo Configure::read('base_url');?>products/details/<?php echo $product['Product']['id']; ?>">
		 <?php echo $html->image('thumbs/'.$thumbnail->render($product['Product']['products_thumbnailImage'],
				           array(
				                'path'=>'thumbs',
				                'width'=>140,
				                'height'=>140,
				                'quality'=>80
				                )
				            ),array("border"=>"0","title"=>$product['Product']['products_title'])
				      );
				?>
                   </a>
		<?php //echo'<img src="'.Configure::read('base_url').'img/uploads/'.$product['Product']['products_thumbnailImage'].'" alt="'.$product['Product']['products_title'].'" />';?></td>
		<td align="center" class="pl_borderbottom"><?php echo $product['Product']['id']; ?></td>
		<td align="center" class="pl_borderbottom"><?php if(date("Y-m-d H:i:s")>$product['Product']['products_expiredDate']){ echo $product['Product']['products_title'];}else{ echo $this->Html->link(__($product['Product']['products_title'], true), array('action' => 'details',$product['Product']['id']));} ?></td>
		<td align="center" class="pl_borderbottom">$<?php echo $product['Product']['products_price']; ?></td>
		<td align="center" class="pl_borderbottom"><?php echo $product['Product']['products_views']; ?></td>
		<td class="actions pl_borderbottom" align="center">
                <a class="showdiv" href="#" style="float:left;"><img src="<?php echo $this->webroot?>img/select_img.png" alt="select image" /></a>
                <div class="hideShowMenu" style="border: 1px solid black; z-index: 2; margin-top: 22px; position: absolute; display: none; background: none repeat scroll 0% 0% rgb(255, 255, 255); padding: 10px; text-align: left;">
                    <a href="<?php echo $this->Html->url(array("controller" => "products","action" => "edit",$product['Product']['id']));?>">Edit</a> <br>
                    <a href="<?php echo $this->Html->url(array("controller" => "products","action" => "sell_similar",$product['Product']['id']));?>">Sell similar</a> <br>
                    <a href="<?php echo $this->Html->url(array("controller" => "products","action" => "end",$product['Product']['id']));?>">End</a> <br>
                    <a href="javascript:void(0)" onClick="addnoteclik(<?php echo $product['Product']['id']; ?>)">add note</a> <br>
					<?php if($product['Product']['featured']=="No"){?>
					<a href="<?php echo $this->Html->url(array("controller" => "products","action" => "featured",1,$product['Product']['id']));?>">Make featured</a> <br>
					<?php }if($product['Product']['featured']=="Yes"){?>
					<a href="<?php echo $this->Html->url(array("controller" => "products","action" => "featured",0,$product['Product']['id']));?>">Remove featured</a> <br>
					<?php }?>
                </div>
                 <?php
                //$options=array('edit'=>'Edit','end'=>'End','sell'=>'Sell Similar','add'=>'Add Note');
                //echo $this->Form->select('action',$options,null,array('empty'=>'--Select--','id'=>$product['Product']['id'],'onchange' => 'redirectaction(this)'));
		?>                   
		</td>
	</tr>
        <tr  <?php //echo $class;?>>
            <td colspan="7" id="disnote<?php echo $product['Product']['id']; ?>" class="clicktoedit">
        <?php
        
        if(!empty ($product['Note'])){
        ?>
        My Note:
    <?php
        $j=1;
        foreach($product['Note'] as $comment) {
        echo '<form method="post" id="inlineformcom'.$comment['id'].'"  name="inlineformcom" action="#"><p>'.$j.'.<label>'.$comment['note'];echo"</label><a href=\"javascript:void(0)\" id=".$comment['id'].">Edit</a></p></form>";
        $j++;
        }
        ?>
       <hr>
        <?php
        }
        ?> </td></tr>
<!--        <tr style="background: none repeat scroll 0pt 0pt BlanchedAlmond;">
            <td id="disnote14" colspan="7" class="fffff">
                My Note:
                <p>1. <label>good product</label><a href="javascript:void(0)">Edit</a></p><p>2. <label>good product two</label><a href="javascript:void(0)">Edit</a></p>       <hr>
         </td>
        </tr>-->
        <tr style="display:none;" id="available<?php echo $product['Product']['id']; ?>">
         <td colspan="7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
             <form method="post" id="fcomment<?php echo $product['Product']['id']; ?>"  name="fcom" action="#">
             <input type="text" name="data[Note][note]" class="required" value="" id="comment<?php echo $product['Product']['id']; ?>" size="75"  style="background: none repeat scroll 0pt 0pt HoneyDew;"/>
             &nbsp;&nbsp;&nbsp;<input class="button" type="button" value="Save" onclick="return savenote(<?php echo $product['Product']['id']; ?>);" />
         &nbsp;&nbsp;&nbsp;<input class="button" type="button" value="Cancel" onclick="return entrycancel(<?php echo $product['Product']['id']; ?>);"/>
             </form>
             </td></tr>

<?php

endforeach;
        }else{
?>

    <tr>
        <td colspan="7">
             No Data Found.
        </td>
	</tr>
       <?php

        }
        ?>
        
        </tbody>
	</table>
                <?php echo $this->Html->css('table_css');?>
<?php  echo $html->script(array("jquery.tablesorter.min.js","jquery.tablednd.js")); ?>
        <style type="text/css">
            .editMode span{ display:none;}

            table.tablesorter tbody td {
    border-bottom: 1px solid #CCCCCC;
    border-right: 0px solid #CCCCCC;
    padding: 0px;
    vertical-align: top;
}
        </style>
        <script type="text/javascript">
            jQuery(document).ready(function(){
              // jQuery("#productlist").tablesorter({headers: { 0:{sorter: false},1:{sorter: false},6:{sorter: false}}});
               var prevtext;
               var commentid;
               jQuery(".clicktoedit p a").live('click', function(){
               commentid=jQuery(this).attr("id");
               //alert(commentid);
                prevtext = jQuery(this).parent().find('label').text();
                jQuery(this).parent().find('label').replaceWith('<input type="text" name="data[Note][note]" size="50" class="takeData required" value="'+prevtext+'" /><button class="saver">Save</button><button class="canceler">Cancel</button></form>');
                jQuery(this).remove();
                //jQuery(this).parent().addClass('editMode');
                jQuery("td.clicktoedit").each(function (index,domEle) {
					  jQuery(domEle).removeClass("clicktoedit");
					  jQuery(domEle).addClass("clickToCancel");
            });
               });

               jQuery(".canceler").live("click", function(){
                   jQuery(this).parent().find('.takeData').replaceWith('<label>'+prevtext+'</label><a id="'+commentid+'" href="javascript:void(0)">Edit</a>');
                   jQuery(this).parent().find('.saver').remove();
                   //jQuery(this).parent().removeClass('editMode')
                   jQuery(this).parent().find('button').remove();
                    jQuery("td.clickToCancel").each(function (index,domEle) {
                        jQuery(domEle).removeClass("clickToCancel");
                        jQuery(domEle).addClass("clicktoedit");
                    });
               });

               jQuery(".saver").live("click", function(){
                   jQuery("td.clickToCancel").each(function (index,domEle) {
                        jQuery(domEle).removeClass("clickToCancel");
                        jQuery(domEle).addClass("clicktoedit");
                    });
                var note=jQuery(this).parent().find(".takeData").val();
                if(note==''){
                return false;
                }
      data = jQuery("#inlineformcom"+commentid).serialize();
      jQuery(this).parent().find('.takeData').replaceWith('<label>'+jQuery(this).parent().find(".takeData").val()+'</label><a id="'+commentid+'" href="javascript:void(0)">Edit</a>');
                jQuery(this).parent().find('button').remove();
      jQuery.ajax({
                type: 'POST',
                url: baseurl+"products/updatenote/"+commentid,
                data:data,
                success: function(){

                }
	});
               });
            });
        </script>
        <div  style="margin-top:3px;">
            <input type="button" class="add_selected" value="Add Note to Selected" name="Add Note to Selected" />&nbsp;
            <input type="button" class="end_selected" value="End Selected Listing" name="End Selected Listing" />&nbsp;
			<input type="button" class="make_featured" value="Make Featured" name="Make Selected Featured" />&nbsp;
			<input type="button" class="remove_featured" value="Remove Featured" name="Remove Selected From Featured" />&nbsp;
        </div>
	<p>
	<?php
	echo $this->Paginator->counter(array(
	'format' => __('Page %page% of %pages%', true)
	));
	?>	</p>

	<div class="paging">
		<?php echo $this->Paginator->prev('< ', array(), null, array('class'=>'disabled'));?>
	 | 	<?php echo $this->Paginator->numbers();?> |
		<?php echo $this->Paginator->next(' >', array(), null, array('class' => 'disabled'));?>
	</div>
</div>
<style type="text/css">
th,th a{font-size:14px; font-weight:700; text-decoration:none;color:black;}
</style>
<?php echo $html->script(array("jquery.validate.js")); ?>

<script type="text/javascript">
jQuery(".showdiv").click(function(){
    jQuery(".hideShowMenu").hide();
    jQuery(this).next().show();
    return false;
});

jQuery("body").click(function(){
    jQuery(".hideShowMenu").hide();
});

jQuery("#select_all").click(function(){
        var checked_status = this.checked;
        jQuery("input[id=products_id]").each(function()
        {
            //var mid=jQuery(this).val();
            this.checked = checked_status;
        });
    });

jQuery(".add_selected").click(function(){
	//var checked_status = this.checked;
	jQuery("input[id=products_id]").each(function()
	{
		var mid=jQuery(this).val();
		var checked_status = this.checked;
		//this.checked = checked_status;
		if(checked_status==true){
			jQuery('#available'+mid).show();
		}
	});
});

jQuery(".end_selected").click(function(){
    var check=false;
        jQuery("input[id=products_id]").each(function()
        {
            //var mid= jQuery(this).val();
            var checked_status=this.checked;
            if(checked_status==true){
                check=true;
            }
        });
        if(check){

            var r=confirm("Do you really want to End the selected Product?");
            if (r){
                //var checked_status = this.checked;
                jQuery("input[id=products_id]").each(function()
                {
                    var mid=jQuery(this).val();
                    var checked_status = this.checked;
                    //this.checked = checked_status;
                    if(checked_status==true){
                        jQuery.ajax({
                            type: 'GET',
                            url : baseurl+"products/endselected/"+mid,
                            dataType: 'html',
							success : function (data) {
								window.location = baseurl+'products/';
							}
						});
					}
				});

        }
    }
});

jQuery(".make_featured").click(function(){
    var data=new Array(); 
	var flag=true;
        jQuery("input[id=products_id]").each(function(index)
        {           
            var checked_status=this.checked;
            if(checked_status==true){
                var mid=jQuery(this).val();
				data[index] = mid;
            }
        });
		d=data.length;
        if(d>0){
			url = baseurl+"products/makefeatured/";                           
			jQuery.post(url, { 'pid[]' : data},function(data) {
			  window.location = baseurl+'products/';
			});
			
		}
		else{alert("You have to select product to make featured");}
});
jQuery(".remove_featured").click(function(){
    var data=new Array(); 
	var flag=true;
        jQuery("input[id=products_id]").each(function(index)
        {           
            var checked_status=this.checked;
            if(checked_status==true){
                var mid=jQuery(this).val();
				data[index] = mid;
            }
        });
		d=data.length;
        if(d>0){        
			url = baseurl+"products/remove_featured/";                           
			jQuery.post(url, { 'pid[]' : data},function(data) {
			  window.location = baseurl+'products/';
			});
			
		}
		else{alert("You have to select product to make featured");}
});

function redirectaction(param){
        var mval=jQuery(param).val();
        var mid=jQuery(param).attr('id');
        ////////delete//////////
        if(mval=='end'){
            var r=confirm("Do you really want to delete the product?");
            if(r){
              window.location = baseurl+'products/delete/'+mid;
            }
          }
      ////////////////////////Edit//////////
        if(mval=='edit'){
           window.location = baseurl+'products/edit/'+mid;
            }
    ////////////////////////add note//////////
        if(mval=='add'){
           //window.location = baseurl+'products/edit/'+mid;
            jQuery('#available'+mid).show();
            }
            ////////////////////////sell similar//////////
        if(mval=='sell'){
           window.location = baseurl+'products/sell_similar/'+mid;
            }
                ////////////////////////unsold//////////
        if(mval=='unsold'){
           window.location = baseurl+'products/unsold/';
            //jQuery('#available'+mid).show();
            }
               ////////////////////////Pending//////////
        if(mval=='pending'){
           window.location = baseurl+'products/pending/';
            //jQuery('#available'+mid).show();
            }
////////////////////////disapproved//////////
        if(mval=='disapproved'){
           window.location = baseurl+'products/disapproved/';
            //jQuery('#available'+mid).show();
            }
    }
function addnoteclik(id){
    ////////////////////////add note//////////        
           jQuery('#available'+id).show();            
    }

 function entrycancel(id){
      jQuery('#available'+id).hide();
 }

 function savenote(id){
      var note=jQuery('#comment'+id).val();
      if(note==''){
           return false;
      }
      jQuery('#available'+id).hide();
      data = jQuery("#fcomment"+id).serialize();
	  //alert(data);
      jQuery.ajax({
                type: 'POST',
                url: baseurl+"products/addnote/"+id,
                data:data,
                success: function(html){
                       document.getElementById("disnote"+id).innerHTML = "";
                       //jQuery("#disnote"+id).append(html);
                       window.location.reload();
                }
	});   
 }
</script>
<style type="text/css">

    table.tablesorter tbody tr.even td {
        border: none;
}
table.tablesorter {
    background-color: #fff;
}
</style>